<?php

include DIRETORIO."app/Models/TeamDAO.php";

class C_Team{

	protected static $lk = NULL;
	
	public static function init(){
		global $db_Mysql;

		self::$lk = $db_Mysql;
	}

	public static function getDB(){
		if(self::$lk == NUll)
			self::init();

		return self::$lk;
	}

	public static function getTeams(){
		$return = null;

		$objTeamDAO	= new TeamDAO(self::getDB());
		$objTeams	= $objTeamDAO->getTeams(0,array(0,1));	
		
		if(count($objTeams) > 0){
			$html = '<ul class="teams">';

			foreach($objTeams as $row){
				$statusName = $row->status == 1?'Ativa':'Inativa';

				$html .= '	<li id="team_'.$row->id.'">
								<div class="description">
									<span class="title">Nome: '.$row->name.'</span>
									<span><strong>'.$row->projects.'</strong> Projeto(s)</span>
									<span>Estado: '.$statusName.'</span>
									<span><strong class="count_users">'.$row->users.'</strong> Usuário(s)</span>
								</div>
								<div class="buttons">
									<a onclick="editTeam('.$row->id.')" href="javascript:void(0);" class="icon edit"><span>editar</span></a>
									<a onclick="deleteTeam('.$row->id.')" href="javascript:void(0);" class="icon delete"><span>deletar</span></a>
								</div>								
							</li>';
			}
			
			$html .= '</ul>';
			$return[] = true;
		}else{
			$html = "<h2>Nenhum registro encontrado!</h2>";
			$return[] = false;
		}
	
		$return[] = $html;	
		return json_encode($return);
	}

	public static function newTeam(){
		$return = null;
		
		$selectStatus = '<option value="1">Ativa</option>';
		$selectStatus .= '<option value="0">Inativa</option>';

		$html = file_get_contents(TEMPLATES."team.html");		
		$html = str_replace("[TITLE]","Nova Equipe",$html);
		$html = str_replace("[ID_TEAM]",0,$html);
		$html = str_replace("[NAME_TEAM]","",$html);
		$html = str_replace("[LIST_STATUS]",$selectStatus,$html);

		if($html <> ""){
			$return = array(true,$html);
		}else{
			$return = array(false,"Não foi possível carregar a Equipe!");
		}

		return json_encode($return);
	}

	public static function editTeam($post){
		$return = null;

		$objTeamDAO	= new TeamDAO(self::getDB());
		$objTeams	= $objTeamDAO->getTeams((int)$post[id]);

		$selectStatus = '<option value="1"'.($objTeams[0]->status == 1?' selected':'').'>Ativa</option>';
		$selectStatus .= '<option value="0"'.($objTeams[0]->status == 0?' selected':'').'>Inativa</option>';

		$html = file_get_contents(TEMPLATES."team.html");		
		$html = str_replace("[TITLE]","Editar Equipe",$html);
		$html = str_replace("[ID_TEAM]",$objTeams[0]->id,$html);
		$html = str_replace("[NAME_TEAM]",$objTeams[0]->name,$html);
		$html = str_replace("[LIST_STATUS]",$selectStatus,$html);

		if($html <> ""){
			$return = array(true,$html);
		}else{
			$return = array(false,"Não foi possível carregar a Equipe!");
		}

		return json_encode($return);
	}

	public static function saveTeam($post){
		$return = null;
		$id		= 0;
		
		if(count($post) > 0){
			$objTeamDAO	= new TeamDAO(self::getDB());
			$objTeam	= new Team();

			foreach($post as $key => $value){
				$objTeam->$key = $value;
			}
						
			if($objTeam->id > 0){		
				$objTeamDAO->save($objTeam);
				$id = $objTeam->id;
			}else{
				$id = $objTeamDAO->save($objTeam);
			}
		}
		

		if($id > 0){
			$return =  array(true,"Equipe <i>\"". $objTeam->name."\"</i> salva com sucesso.");
		}else{
			$return =  array(false,"Não foi possível salvar a Equipe!");
		}


		return json_encode($return);
	}

	public static function getUsersTeam(){
		$objTeamDAO	= new TeamDAO(self::getDB());

		$result = $objTeamDAO->getUsersTeam();

		if(count($result) > 0){
			$return = array(true,$result);
		}else{
			$return =  array(false,"Não foi possível atualizar as Equipes!");
		}

		return json_encode($return);
	}

	public static function deleteTeam($post){	
		$objTeamDAO	= new TeamDAO(self::getDB());
		$idTeam		= (int)$post['id'];
		$result		= 0;

		if($idTeam > 0)
			$result = $objTeamDAO->deleteTeam($idTeam);

		if($result > 0)
			$return = array(true,"Equipe excluída com sucesso!");
		else if($result == -1)
			$return = array(false,"Erro, existem Projetos vinculados a esta Equipe!");
		else
			$return = array(false,"Erro, não foi possível excluir a Equipe!");

		return json_encode($return);
	}
}
?>